package com.xmall.service.impl;

import com.github.pagehelper.PageHelper;
import com.xmall.bean.table.Comment;
import com.xmall.bean.table.CommentExample;
import com.xmall.bean.table.User;
import com.xmall.bean.table.UserExample;
import com.xmall.bean.vo.WxCommentListVo1;
import com.xmall.bean.vo.WxCommentListVo2;
import com.xmall.bean.vo.WxCommentListVo3;
import com.xmall.mapper.CommentMapper;
import com.xmall.mapper.UserMapper;
import com.xmall.service.WxCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class WxCommentServiceImpl implements WxCommentService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    CommentMapper commentMapper;

    @Override
    public WxCommentListVo1 wxCommentList(Integer valueId,
                                          Byte type,
                                          Integer size,
                                          Integer page,
                                          Integer showType) {
        PageHelper.startPage(page, size);//分页
        CommentExample commentExample = new CommentExample();
        commentExample.setOrderByClause("add_time desc");//按时间降序排列
        CommentExample.Criteria criteria = commentExample.createCriteria();
        if(showType == 1){//判断是否有图
            criteria.andHasPictureEqualTo(true);
        }//查询条件 valueId和type
        criteria.andValueIdEqualTo(valueId)
                .andDeletedEqualTo(false)
                .andTypeEqualTo(type);
        //查询到所有评论
        List<Comment> comments = commentMapper.selectByExample(commentExample);
        WxCommentListVo1 wxCommentListVo1 = new WxCommentListVo1();
        wxCommentListVo1.setCount(comments.size());//封装count
        wxCommentListVo1.setCurrentPage(page);//封装CurrentPage

        //封装list<WxCommentListVo2> 遍历查询单条数据再add
        List<WxCommentListVo2> data = new ArrayList<>();
        List<WxCommentListVo3> wxCommentListVo3s = new ArrayList<>();

        //此遍历得到单个WxCommentListVo2对象
        for (Comment comment : comments) {

            WxCommentListVo2 wxCommentListVo2 = new WxCommentListVo2();
            wxCommentListVo2.setAddTime(comment.getAddTime());
            wxCommentListVo2.setPicList(comment.getPicUrls());
            wxCommentListVo2.setContent(comment.getContent());

            //查询user得到List<WxCommentListVo3> wxCommentListVo3s
            Integer userId = comment.getUserId();
            UserExample userExample = new UserExample();
            userExample.createCriteria().andIdEqualTo(userId);
            //虽然显示为一个users 但是其实之查出了一个User对象 userId唯一
            List<User> users = userMapper.selectByExample(userExample);
            for (User user : users) {
                WxCommentListVo3 wxCommentListVo3 = new WxCommentListVo3();
                wxCommentListVo3.setNickName(user.getNickname());
                wxCommentListVo3.setAvatarUrl(user.getAvatar());
                wxCommentListVo2.setUserInfo(wxCommentListVo3);
            }
            //得到data:List<>
            data.add(wxCommentListVo2);
        }
        wxCommentListVo1.setData(data);
        return wxCommentListVo1;
    }

    @Override
    public HashMap<String, Integer> wxCommmentCount(Integer valueId, Byte type) {
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andValueIdEqualTo(valueId).andTypeEqualTo(type).andDeletedEqualTo(false);

        List<Comment> comments = commentMapper.selectByExample(commentExample);
        Integer allCount = comments.size();
        criteria.andHasPictureEqualTo(true);
        List<Comment> comment1 = commentMapper.selectByExample(commentExample);
        Integer hasPicCount = comment1.size();

        HashMap<String, Integer> map = new HashMap<>();
        map.put("hasPicCount", hasPicCount);
        map.put("allCount", allCount);
        return map;
    }
}
